﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.DTO.WarehouseManagement;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 库存盘点控制器
    /// </summary>
    public class InventoryCountsController : Controller
    {
        /// <summary>
        /// 库存盘点接口
        /// </summary>
        private readonly IInventoryCountsServices inventoryCountsServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="inventoryCountsServices"></param>
        public InventoryCountsController(IInventoryCountsServices inventoryCountsServices)
        {
            this.inventoryCountsServices = inventoryCountsServices;
        }

        public IActionResult InventoryCountsView()
        {
            return View();
        }

        /// <summary>
        /// 列表页面（默认路由）
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View("InventoryCountsView");
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Edit()
        {
            return View("InventoryCountsEditView");
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="inventoryCounts"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddInventoryCounts([FromBody]InventoryCountsDTO inventoryCounts)
        {
        return await inventoryCountsServices.AddInventoryCounts(inventoryCounts);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DelInventoryCounts([FromBody]List<int> ids)
        {
            await inventoryCountsServices.DelInventoryCounts(ids);
        }
        /// <summary>
        /// 获取库存盘点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<InventoryCountsDTO> GetInventoryCounts(int id)
        {
            return await inventoryCountsServices.GetInventoryCounts(id);
        }
        /// <summary>
        /// 库存盘点显示列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="countingOrderName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> ShowInventoryCounts(int pageIndex = 1, int pageSize = 10, string? countingOrderName = "")
        {
            var list= await inventoryCountsServices.ShowInventoryCounts(pageIndex, pageSize, countingOrderName);
            var result = list.OrderByDescending(m => m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code=0,
                msg="",
                count=list.Count,
                data=result
            });
        }
        /// <summary>
        /// 编辑库存盘点
        /// </summary>
        /// <param name="inventoryCounts"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateInventoryCounts([FromBody]InventoryCountsDTO inventoryCounts)
        {
            return await inventoryCountsServices.UpdateInventoryCounts(inventoryCounts);
        }




    }
}
